//
//  WBHomeController.swift
//  CustomWeiBo
//
//  Created by laiqiang on 21/5/9.
//  Copyright © 2021年 laiqiang. All rights reserved.
//

import UIKit

//全局常量－原创微博
private let cellid = "cellid"

//全局常量－被转发微博
private let retweetedcellid = "retweetedcellid"

class WBHomeController: WBBasController {


    //列表视图模型
    fileprivate lazy var listViewModel = WBStatusListViewModel()

    //加载数据
    override func loaddata() {
        
        refreshController?.beginRefreshing()
        
        //模拟网络延迟加载
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
            
            //网络工具加载 微博数据
            self.listViewModel.loadStatus(pullup: self.isPullup) { (isSucces, hasMorepull) in
                
                self.refreshController?.endRefreshing()
                //恢复上拉刷新
                self.isPullup = false
                //存在更多数据时，才刷新页面
                if hasMorepull {
                    
                    self.tableView?.reloadData()
                    print("数据加载结束 数量：\(self.listViewModel.statusList.count)文本：\(self.listViewModel.statusList.last?.status.text)")
                }
            }
        }
    }
    
    @objc func showFriends(){

        let live = testViewController()
        live.hidesBottomBarWhenPushed = true
        navigationController?.pushViewController(live, animated: true)
        
    }

}

//MARK:表格数据
extension WBHomeController{
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        //取出重用的cell
        let viewmodel = listViewModel.statusList[indexPath.row]
        let acellid = (viewmodel.status.retweeted_status != nil) ? retweetedcellid : cellid

        let cell = tableView.dequeueReusableCell(withIdentifier: acellid, for: indexPath) as! WBStatusCell

        cell.setupviewModel(viewModel: viewmodel)
        
        return cell
    }
}

//MARK:设置页面
extension WBHomeController {
    //重写父类方法 -- 设置表示图，如果没有登录，基类回走创建访客视图，不会调用设置表哥视图，这里就不会走。
    override func setupTableView() {
        super.setupTableView()
        
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", style:.plain, target: self, action: #selector(showFriends))
        
        //注册cell
        tableView?.register(UINib(nibName: "WBStatusNormalCell", bundle: nil), forCellReuseIdentifier: cellid)
        tableView?.register(UINib(nibName: "WBStatusRetweetedCell", bundle: nil), forCellReuseIdentifier: retweetedcellid)
        
        tableView?.rowHeight = UITableViewAutomaticDimension
        tableView?.estimatedRowHeight = 300
        
        setupNavTile()
        
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        let vm = listViewModel.statusList[indexPath.row]

        return vm.rowHeight
    }
    
    fileprivate func setupNavTile(){
    
        let title = WBNetworkingManager.share.userAccount.screen_name
        let btn = WBTitleButtun(title: title)
        btn.addTarget(self, action: #selector(clickTitleButtun(btn:)), for: .touchUpInside)
        navItem.titleView = btn
    }
    
    @objc func clickTitleButtun(btn:UIButton){
    
        btn.isSelected = !btn.isSelected
    }
}


















